package cn.edu.hbvtc.qms.web.struts.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import cn.edu.hbvtc.qms.domain.Department;
import cn.edu.hbvtc.qms.service.IManagementService;
import cn.edu.hbvtc.qms.web.struts.form.DepartmentActionForm;

public class DepartmentAction extends AbstractBaseDispatchAction {

	private IManagementService managementService = null;

	public IManagementService getManagementService() {
		return managementService;
	}

	public void setManagementService(IManagementService managementService) {
		this.managementService = managementService;
	}

	// 跳转到部门管理列表页面
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<Department> deptList = managementService.findDepartments();
		request.setAttribute("deptList", deptList);

		return mapping.findForward("deptList");
	}

	// 跳转到部门添加页面
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("deptAdd");
	}

	// 完成到部门添加操作
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DepartmentActionForm daf = (DepartmentActionForm) form;

		// 判断当前输入的院系编号是否合法！
		if (daf.getDeptId() == null || "".equals(daf.getDeptId())) {
			
			request.setAttribute("deptIdError", "请输入院系编号！");
			return mapping.findForward("deptAdd");
		} 
		else if (managementService.findDepartmentByDeptId(daf.getDeptId()) != null) {

			request.setAttribute("deptIdRequest", "该编号已存在，请重新输入！");

			return mapping.findForward("deptAdd");
		}

		if(daf.getDeptName() == null || "".equals(daf.getDeptName())){
			request.setAttribute("deptNameError", "请输入院系名称！");
			
			return mapping.findForward("deptAdd");
		}
		
		Department dept = new Department();
		PropertyUtils.copyProperties(dept, daf);
		managementService.saveDepartment(dept);

		List<Department> deptList = managementService.findDepartments();
		request.setAttribute("deptList", deptList);

		return mapping.findForward("deptList");
	}

	// 跳转到部门修改页面
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DepartmentActionForm daf = (DepartmentActionForm) form;
		Department dept = managementService.findDepartmentById(daf.getId());
		request.setAttribute("dept", dept);

		return mapping.findForward("deptUpdate");
	}

	// 保存部门修改数据
	public ActionForward updateSave(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DepartmentActionForm daf = (DepartmentActionForm) form;
		
		// 判断当前输入的院系编号是否合法！
		if( ! daf.getDeptId() .equals(managementService.findDepartmentById(daf.getId()).getDeptId())){ //判断提交的院系编号是否被用户修改
	
		if (daf.getDeptId() == null || "".equals(daf.getDeptId())) {
			
			Department dept = managementService.findDepartmentById(daf.getId());
			request.setAttribute("dept", dept);
			
			request.setAttribute("deptIdError", "请输入院系编号！");
			return mapping.findForward("deptUpdate");
		} 
		else if (managementService.findDepartmentByDeptId(daf.getDeptId()) != null) {

			Department dept = managementService.findDepartmentById(daf.getId());
			request.setAttribute("dept", dept);
			
			request.setAttribute("deptIdRequest", "该编号已存在，请重新输入！");

			return mapping.findForward("deptUpdate");
		}
		}
		if(daf.getDeptName() == null || "".equals(daf.getDeptName())){
			Department dept = managementService.findDepartmentById(daf.getId());
			request.setAttribute("dept", dept);
			
			request.setAttribute("deptNameError", "请输入院系名称！");
			
			return mapping.findForward("deptUpdate");
		}
		
		Department dept = managementService.findDepartmentById(daf.getId());
		PropertyUtils.copyProperties(dept, daf);
		managementService.updateDepartment(dept);

		List<Department> deptList = managementService.findDepartments();
		request.setAttribute("deptList", deptList);

		return mapping.findForward("deptList");
	}

	// 完成部门删除操作
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DepartmentActionForm daf = (DepartmentActionForm) form;
		managementService.deleteDepartmentById(daf.getId());

		List<Department> deptList = managementService.findDepartments();
		request.setAttribute("deptList", deptList);

		return mapping.findForward("deptDel");
	}
}
